﻿#region Copyright © 2010 Chad Albrecht & Mark Adams
/*
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.*/

/* © BLOKUS and associated trademarks are copyright Mattel, Inc.   */
#endregion

//#define _TEST_WIN

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Blokus
{
    public class GameController
    {
        protected GameBoard board;
        protected TileWell tw1;
        protected TileWell tw2;

        public delegate void GameOverHandler(object sender);
        public event GameOverHandler GameOver;

        public GameController(GameBoard board, TileWell tw1, TileWell tw2)
        {
            this.board = board;
            this.tw1 = tw1;
            this.tw2 = tw2;

            this.tw1.IsTurn = true;
            this.tw1.BringToTop();
            this.tw1.TurnFinished += new TileWell.TurnFinishedHandler(tw1_TurnFinished);
            this.tw2.TurnFinished += new TileWell.TurnFinishedHandler(tw2_TurnFinished);
        }

        void tw2_TurnFinished(object sender)
        {
            bool Player1Dead = tw1.IsDead;
            bool Player2Dead = tw2.IsDead;

            if (!Player1Dead)
            {
                this.tw1.IsTurn = true;
                this.tw2.IsTurn = false;

                this.tw1.BringToTop();
                this.tw2.SendToBack();
            }

            CheckForGameOver(Player1Dead, Player2Dead);
        }

        void tw1_TurnFinished(object sender)
        {
            bool Player1Dead = tw1.IsDead;
            bool Player2Dead = tw2.IsDead;
            
            if (!Player2Dead)
            {
                this.tw1.IsTurn = false;
                this.tw2.IsTurn = true;

                this.tw2.BringToTop();
                this.tw1.SendToBack();
            }

            CheckForGameOver(Player1Dead, Player2Dead);
        }

#if _TEST_WIN
        private int _turns = 0;
#endif

        private void CheckForGameOver(bool Player1Dead, bool Player2Dead)
        {
            bool AllPlayed = (tw1.Score ==0 ) || (tw2.Score == 0);
            bool BothDead = Player1Dead && Player2Dead;
            if ((AllPlayed || BothDead) && (GameOver != null))
                GameOver(this);    
#if _TEST_WIN
            if(_turns++ == 4)
                GameOver(this);
#endif
        }

        public void RestartGame()
        {                       
            this.board.Reset();

            this.tw1.IsTurn = true;
            this.tw2.IsTurn = false;

        }

        public int getPlayer1Score()
        {
            return tw1.Score;
        }

        public int getPlayer2Score()
        {
            return tw2.Score;
        }
    }
}
